<template>
  <div class="detailContent">
    <div class="downloadBox" v-show="tableList.length > 0">
      <!-- <span class="download" @click="downloadTable">下载表格</span> -->
      <el-button
        type="primary"
        @click="downloadTable"
        class="commonBut saveBtn funComBut"
      >
        <span class="opetareText">下载表格</span>
      </el-button>
    </div>
    <el-table
      :data="tableList"
      v-loading="loading"
      :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
      highlight-current-row
      stripe
      style="border: 1px solid #dfe6ec; margin-top: 24px"
    >
      <!-- <el-table-column
                prop="indexName"
                label="指标名称"
                align="left"
                :formatter="formatTd"
                min-width="15%"
            ></el-table-column> -->
      <el-table-column label="指标名称" width="230">
        <template slot-scope="scope">
          <copyText
            :tableIndex="scope.$index"
            :copyTextName="scope.row.indexName"
            :columnName="'indexName'"
          ></copyText>
        </template>
      </el-table-column>
      <!-- <el-table-column
                label="指标算法"
                align="center"
                min-width="35%"
            >
                <template slot-scope="scope">
                    <div
                        v-html="scope.row.calculateMethod"
                        class="howRepair"
                    ></div>
                </template>
            </el-table-column> -->
      <el-table-column label="指标算法" width="230">
        <template slot-scope="scope">
          <copyText
            :tableIndex="scope.$index"
            :copyTextName="scope.row.calculateMethod"
            :columnName="'calculateMethod'"
          ></copyText>
        </template>
      </el-table-column>
      <el-table-column
        prop="fullMarks"
        label="满分"
        align="center"
        :formatter="formatTd"
        min-width="10%"
      ></el-table-column>
      <el-table-column
        prop="actualScore"
        label="实际得分"
        align="center"
        :formatter="formatTd"
        min-width="10%"
      ></el-table-column>
      <el-table-column label="修复项" align="center" min-width="28%">
        <template slot-scope="scope">
          <div>
            {{
              scope.row.actualScore * 1 === scope.row.fullMarks * 1
                ? '无'
                : scope.row.reason
                ? scope.row.reason.indexOf('2、扣分明细') > -1
                  ? scope.row.reason
                      .split('2、扣分明细')[0]
                      .split('扣分原因：')[1]
                  : scope.row.reason.indexOf('扣分原因：') > -1
                  ? scope.row.reason.split('扣分原因：')[1]
                  : scope.row.reason
                : '--'
            }}
          </div>
          <el-link
            type="primary"
            v-if="
              scope.row.reason && scope.row.reason.indexOf('2、扣分明细') > -1
            "
            @click="showScoreDetail(scope.row.id, scope.row)"
            >( 查看明细 )</el-link
          >
        </template>
      </el-table-column>
      <!-- 表格数据为空时，页面显示 -->
      <div slot="empty" v-show="tableList.length < 1">
        <emptyTable emptyText="暂无评价相关数据"></emptyTable>
      </div>
    </el-table>
    <ReduceDialog
      :scoreDetailVisible="scoreDetailVisible"
      :reduceScoreDetail="reduceScoreDetail"
      @closeDialog="closeDialog"
    ></ReduceDialog>
  </div>
</template>

<script>
import ReduceDialog from './ReduceDialog'
import common from '../../kits/common'

export default {
  name: 'ESreduceTable',
  components: {
    ReduceDialog,
  },
  props: {
    tableList: {
      type: Array,
      default() {
        return []
      },
    },
    companyProjectScore: {
      type: Object,
      default() {
        return {}
      },
    },
    scoreType: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      scoreDetailVisible: false, //扣分明细弹窗
      reduceScoreDetail: {
        detailId: '',
        type: '',
        reduceScoreDetailInfo: [],
      },
      specificData: {},
    }
  },
  methods: {
    // 扣分明细弹窗
    showScoreDetail(detailId, row) {
      this.reduceScoreDetail.detailId = detailId
      this.specificData = row
      return this.axios({
        method: 'post',
        url: 'api-ps/creditDeductionDetail/expand/getCreditDedDetail',
        data: {
          detailId,
        },
      })
        .then((response) => {
          if (response.data.hasError) {
            this.$message.error(response.data.errorMsg)
          } else {
            const results = response.data.result
            this.reduceScoreDetail.type = results.type
            if (results.type == '1') {
              this.handleData(results, 'names')
            } else if (results.type == '2') {
              this.handleData(results, 'recordTimes')
            } else {
              this.handleData(results, 'timeAndName')
            }
            this.scoreDetailVisible = true
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
    // 处理明细数据
    handleData(results, detailName) {
      if (
        this.specificData.reason &&
        this.specificData.reason.indexOf('扣分明细：') > -1
      ) {
        let reduceStr = this.specificData.reason.split('扣分明细：')[1]
        if (reduceStr.indexOf('b') > -1) {
          // 存在未代发或补发或两条明细（A为补发）
          let reduceReason = this.specificData.reason
            .split('扣分明细：')[0]
            .split('扣分原因：')[1]
          let detailInfo = {}
          if (reduceReason.split('/')[0].indexOf('存在0') === -1) {
            detailInfo.title = reduceStr.split('b')[0].split('a')[1]
            let notIssued = null
            notIssued = results[detailName].filter((v) => {
              if (v.indexOf('A') === -1) {
                return v
              }
            })
            detailInfo.detail = notIssued
            this.reduceScoreDetail.reduceScoreDetailInfo.push(
              JSON.parse(JSON.stringify(detailInfo))
            )
          }
          if (reduceReason.split('/')[1].indexOf('存在0') === -1) {
            detailInfo = {}
            detailInfo.title = reduceStr.split('b')[1]
            let reissue = []
            results[detailName].forEach((v) => {
              if (v.indexOf('A') > -1) {
                reissue.push(v.split('A')[1])
              }
            })
            detailInfo.detail = reissue
            this.reduceScoreDetail.reduceScoreDetailInfo.push(
              JSON.parse(JSON.stringify(detailInfo))
            )
          }
        } else {
          this.reduceScoreDetail.reduceScoreDetailInfo[0] = {}
          this.reduceScoreDetail.reduceScoreDetailInfo[0].title = reduceStr
          if (detailName === 'timeAndName') {
            let detailArr = []
            let detailNameArr = Object.keys(results[detailName])
            for (let i = 0; i < detailNameArr.length; i++) {
              detailArr[i] = {}
              detailArr[i].month = detailNameArr[i]
              detailArr[i].name = JSON.parse(
                JSON.stringify(results[detailName][detailNameArr[i]])
              )
            }
            this.reduceScoreDetail.reduceScoreDetailInfo[0].detail = JSON.parse(
              JSON.stringify(detailArr)
            )
          } else {
            this.reduceScoreDetail.reduceScoreDetailInfo[0].detail = JSON.parse(
              JSON.stringify(results[detailName])
            )
          }
        }
      }
    },
    closeDialog() {
      this.scoreDetailVisible = false
      this.reduceScoreDetail.reduceScoreDetailInfo = []
    },
    // 下载表格
    downloadTable() {
      if (this.scoreType === 'company') {
        if (
          this.$store.state.userType == 'PROJECT_ADMINISTRATOR' ||
          this.$store.state.userType == 'LABOR_ADMINISTRATOR'
        ) {
          // 项目端导出企业评分
          window.location.href =
            common.commonApi +
            'api-ps/creditComprojScorDel/expand/exportCPScoreDetailES?companyProjectScoreId=' +
            this.companyProjectScore.id +
            '&type=' +
            this.companyProjectScore.type +
            '&projectId=' +
            this.companyProjectScore.projectId +
            '&uscc=' +
            this.companyProjectScore.uscc +
            '&access_token=' +
            localStorage.getItem('access_token')
        } else {
          // 监察/企业端导出企业评分
          window.location.href =
            common.commonApi +
            'api-ps/creditComprojScorDel/expand/exportCPScoreDetailES?companyProjectScoreId=' +
            this.$route.query.companyProjectScoreId +
            '&type=' +
            this.$route.query.type +
            '&projectId=' +
            this.$route.query.projectId +
            '&uscc=' +
            this.$route.query.uscc +
            '&access_token=' +
            localStorage.getItem('access_token')
        }
      } else {
        // 导出项目评分
        window.location.href =
          common.commonApi +
          'api-ps/creditProjScorDetail/expand/exportPScoreDetailES?projectScoreId=' +
          this.$route.query.projectScoreId +
          '&appType=20' +
          '&projectId=' +
          this.$route.query.projectId +
          '&access_token=' +
          localStorage.getItem('access_token')
      }
    },
  },
}
</script>

<style scoped lang="less">
.detailContent {
  // position: relative;
  .downloadBox {
    zoom: 1; /*处理兼容*/
    &:after {
      content: '';
      display: block;
      clear: both;
      height: 0;
      overflow: hidden;
      visibility: hidden; /*处理浏览器兼容;*/
    }
    .download {
      width: 100px;
      height: 32px;
      background: rgb(2, 166, 239);
      text-align: center;
      line-height: 32px;
      color: #fff;
      float: right;
      cursor: pointer;
      font-size: 14px;
    }
  }
}
.howRepair {
  text-align: left;
}
</style>
